//
//  ProTableViewController.m
//  全国天气解析
//
//  Created by 胡先剑 on 16/3/27.
//  Copyright © 2016年 胡先剑. All rights reserved.
//

#import "TableViewController.h"

@interface TableViewController ()<NSXMLParserDelegate> //支持XML协议

/** 保存解析得到的数据 */
@property (nonatomic,strong) NSMutableArray *list;

@end

@implementation TableViewController

#pragma mark - 懒加载
- (NSMutableArray *)list
{
    if (_list == nil)
    {
        _list = [[NSMutableArray alloc] init];
    }
    return _list;
}

#pragma mark - 视图加载
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    /* 设置导航栏标题 */
    self.navigationItem.title = [NSString stringWithFormat:@"%@天气",self.name];
    
    /* 设置导航栏返回按钮 */
    if (![self.name isEqualToString:@"中国"])
    {
        UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"返回%@",self.backName] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
        self.navigationItem.leftBarButtonItem = back;
    }
    
    /* 获取数据 */
    NSString *str = [NSString stringWithFormat:@"http://flash.weather.com.cn/wmaps/xml/%@.xml",self.pyName];
    NSURL *url = [NSURL URLWithString:str];
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    /* 创建解析器 */
    NSXMLParser *parper = [[NSXMLParser alloc] initWithData:data];
    parper.delegate = self;
    
    /* 解析开始 */
    [parper parse];  
}

#pragma mark - 导航栏返回按钮触发的事件
- (void)back
{
    /* 返回导航栏上个界面 */
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 解析到某个标记时触发的方法
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict
{
    /* 将解析到的字典保存 */
    if ([elementName isEqualToString:@"city"])
    {
        [self.list addObject:attributeDict];
    }
}

#pragma mark - 设置表格行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

#pragma mark - 设置表格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* 注册单元格 */
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    
    /* 设置每个省的天气 */
    NSDictionary *dic = self.list[indexPath.row];
    cell.textLabel.text = dic[self.nameKey];
    NSString *str = [NSString stringWithFormat:@"%@  %@°C ~ %@°C  %@",dic[@"stateDetailed"],dic[@"tem1"],dic[@"tem2"],dic[@"windState"]];
    cell.detailTextLabel.text = str;
    
    return cell;
}

#pragma mark - 选中表格某行时触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* 取消表格当前选中状态 */
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    /* 创建新的页面 */
    NSDictionary *dic = self.list[indexPath.row];
    if (![dic[@"pyName"] isEqualToString:@""])
    {
        TableViewController *table = [[TableViewController alloc] init];
        table.name = dic[self.nameKey];
        table.pyName = dic[@"pyName"];
        table.nameKey = @"cityname";
        table.backName = self.name;
        [self.navigationController pushViewController:table animated:YES];
    }
}

@end
